function [nmodel]=i_cruise(dat,model)

% -----------------------------------------------------------
% -- Interpolating modelled fields on observation's position
% -- in the "cruise" case
% -----------------------------------------------------------
% G. Charria (10/2008)


% ---- Create the Structure ...
nmodel = struct('dummy',1);
    

% ---- Control that the model is covering the same period as the data
if (min(model.time) > max(max(dat.timedata))) || (max(model.time) < min(min(dat.timedata)))
    errordlg('The model is not covering the same period as data. Please, try to use a different year')
    return
else
    % ---- I. Interpolation over the cruise track (lon,lat) ----
    % ---- Control of longitude dimension
    if issorted(dat.londata(1,:))
        lo=dat.londata(:,1);
    else
        lo=dat.londata(1,:);
    end
    
    
    % ---- Control of latitude dimension
    if issorted(dat.latdata(1,:))
        la=dat.latdata(:,1);
    else
        la=dat.latdata(1,:);
    end
    %plot(lo,la,'r+')
    
    
    % ---- Control of time and depth dimensions
    if issorted(dat.timedata(1,:))
        tt=dat.timedata(:,1);
        zz=size(dat.timedata,2);
        ztest=1;
    else
        tt=dat.timedata(1,:);
        zz=size(dat.timedata,1);
        ztest=2;
    end
    
    % ---- Interpolation    
    d1(1:size(model.data,1),1:size(model.data,4),1:length(lo))=NaN;       
    d2(1:size(model.data,1),1:length(tt),1:length(lo))=NaN;
    h=waitbar(0,'Horizontal and temporal interpolation ...');
    for iz=1:size(model.data,1)
        for it=1:size(model.data,4)
            waitbar(   ((iz-1)*(size(model.data,4)+length(lo)) + it)/((size(model.data,4)+length(lo))*size(model.data,1)),h) 
            d1(iz,it,:)=griddata(model.lon,model.lat,squeeze(model.data(iz,:,:,it)),lo,la);
            % ---
            %figure
            %subplot(1,2,1)
            %pcolor(model.lon,model.lat,squeeze(model.data(iz,:,:,it))); shading flat; colorbar;
            %hold on
            %plot(lo,la,'w+')
            %subplot(1,2,2)
            %plot(la,squeeze(d1(iz,it,:)),'+')
            %hold on
            %for ilo=1:length(lo)
            %    matdist=distgrid(model.lon,model.lat,lo(ilo),la(ilo));
            %    [I,J]=find(matdist==min(min(matdist)));
            %    plot(la(ilo),squeeze(model.data(iz,I(1),J(1),it)),'ro')
            %end
            %pause
        end
        
        %whos
        
        % ---- II. Interpolation over the cruise data (time) ----
        d1(isnan(d1))=0.;
        for ilo=1:length(lo)
            waitbar(   ((iz-1)*(size(model.data,4)+length(lo)) + size(model.data,4) + ilo)/((size(model.data,4)+length(lo))*size(model.data,1)),h)             
            ind=~isnan(squeeze(d1(iz,:,ilo)));
            if ~isempty(find(ind>0,1))
                d2(iz,:,ilo)=interp1(model.time(ind),squeeze(d1(iz,ind,ilo)),tt);
                % ---
                %figure
                %plot(dat.timedata,squeeze(d2(iz,:,ilo)),'+')
                %hold on
                %plot(model.time,squeeze(d1(iz,:,ilo)),'or')
                %pause                
            end
        end        
    end
    close(h)        
    
    % ---- Reshape the matrix to have the mode corresponding to the right
    % position ANT time
    d3(1:size(model.data,1),1:length(tt))=NaN;
    for ii=1:length(tt)
        d3(:,ii)=squeeze(d2(:,ii,ii));
    end
    
    % ---- III. Interpolation over the cruise depths ----
    h=waitbar(0,'Vertical interpolation ...');    
    d4(1:zz,1:length(tt))=NaN;
    for it=1:length(tt)
        waitbar(it/length(tt),h)
        if ztest==1
            d4(:,it)=interp1(model.depth,squeeze(d3(:,it)),dat.depthdata(it,:));
        elseif ztest==2            
            d4(:,it)=interp1(model.depth,squeeze(d3(:,it)),dat.depthdata(:,it));
        end
        % ---
        %figure
        %plot(dat.depthdata(it,:),squeeze(d4(:,it)),'+')
        %hold on
        %plot(model.depth,squeeze(d3(:,it)),'or')
        %pause
    end
    close(h)
            
    nmodel.lon=dat.londata;
    nmodel.lat=dat.latdata;
    nmodel.data=d4';
    nmodel.depth=dat.depthdata;
    nmodel.time=dat.timedata;
    
    
    
end

% ---- Clean the Structure ...
nmodel=rmfield(nmodel,'dummy');    
